package com.java80.springmall.cloud.order.consumer.service.impl;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.codingapi.tx.annotation.TxTransaction;
import com.java80.springmall.cloud.order.consumer.client.OrderClient;
import com.java80.springmall.cloud.order.consumer.client.ProductClient;
import com.java80.springmall.cloud.order.consumer.entity.Orders;
import com.java80.springmall.cloud.order.consumer.entity.Product;
import com.java80.springmall.cloud.order.consumer.service.OrderService;
@Service
public class OrderServiceImpl implements OrderService{
	@Autowired
	private OrderClient orderClient;
	@Autowired
	private ProductClient productClient;
	@Override
	@TxTransaction(isStart=true)
	public void createOrder(Long productId, Integer nums, Long userId) {
		Product product = productClient.get(productId);
		BigDecimal price = product.getPrice();
		if(nums>product.getCount()){
			//库存不足。。。
			return;
		}
		Orders o=new Orders();
		o.setAmount(price.multiply(new BigDecimal(nums)));
		o.setNum(nums);
		o.setProductId(productId);
		o.setUserId(userId);
		orderClient.createOrder(o);
		productClient.updateStored(productId, -nums);
		int i=8/0;//人为异常 检查是否会回滚
	}
	
}
